﻿using LPBToolsLib;
using StationNF;
using StationSer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PLCConsole.PLCSerStation
{
    public class Station_ADP2203VX : Station_PLCBase
    {
        public const int StationRegStar = 0x10;
        public const int StationRegCount = 11;
        public const int InputCount = 12;
        public const int ADCCount = 9;

        public override bool isReady
        {
            get
            {
                if (base.isReady)
                {
                    return (StationRegStar == tSourStation.StationRegStar) &&
                        (StationRegCount == tSourStation.StationRegCount);
                }
                return false;
            }
        }

        public Station_ADP2203VX(IFStation SourStation) : base(SourStation)
        {
            StationType = StationPLCType.ADP2203VX;
        }

        public Station_ADP2203VX(IFStationsServer SourStaServer, string DestStaID) : base(SourStaServer, DestStaID)
        {
            StationType = StationPLCType.ADP2203VX;
        }
        public bool getXValue(int Index)
        {
            if ((Index >= 0) && (Index < InputCount)&& isReady)
            {
                if (tSourStation != null)
                {
                    int iInputReg = tSourStation.RegValues[0];
                    return ((iInputReg >> Index) & 1) != 0;
                }
            }
            return false;
        }

        public bool[] IOXValues
        {
            get
            {
                List<bool> tValues = new List<bool>();
                for (int i = 0; i < InputCount; i++)
                    tValues.Add(getXValue(i));
                return tValues.ToArray();
            }
        }
        // 返回AD结果，单位：mA
        public float getADCValue(int Index)
        {
            if ((Index >= 0) && (Index < ADCCount) && isReady)
            {
                if (tSourStation != null)
                {
                    int iInputReg = tSourStation.RegValues[1 + Index];
                    return iInputReg;
                }
            }
            return 0;
        }

        /// <summary>
        /// ADC通道值，实际为通道的电流值，单位：uA
        /// </summary>
        public float[] ADC_uA
        {
            get
            {
                List<float> tValues = new List<float>();
                for (int i = 0; i < ADCCount; i++)
                    tValues.Add(getADCValue(i));
                return tValues.ToArray();
            }
        }

        // 返回主板上供电电压，单位：V
        public float VCC_mV
        {
            get
            {
                if (isReady)
                {
                    int iInputReg = tSourStation.RegValues[10];
                    return iInputReg;
                }
                return 0;
            }
        }
    }
}
